const {
	db,
	dbCmd,
	result_success,
	result_error,
	appletInfoCollection,
	collectionCollection,
	userCollection,
	getCommonTime
} = require('../../common/constants')
module.exports = async function(params = {}) {
	const token = this.getUniIdToken()
	if (!token) {
		return result_error('登录失效,请重新登录')
	}
	let payload = await this.uniID.checkToken(token)
	if (payload.code) {
		return payload
	}
	let type = params.type
	let way = params.way
	let id = params.id
	let cUser = await userCollection.doc(payload.uid).get()
	cUser = cUser.data[0]
	//获取当前应用
	let appletInfoData = await appletInfoCollection.doc(cUser.app_id).get()
	appletInfoData = appletInfoData.data[0]
	let cDate = getCommonTime();
	let cYear = cDate.now.year;
	let cMonth = cDate.now.month;
	let cDay = cDate.now.day;
	let shareCount = 1
	let shareType = 'vip'
	let shareLogs = await db.collection('wmzmall-share-logs').where({
		shop_id: appletInfoData._id,
		year: cYear,
		month: cMonth,
		day: cDay,
		type: shareType
	}).get()
	shareLogs = shareLogs.data[0]
	if (!shareLogs) {
		await db.collection('wmzmall-share-logs').add({
			shop_id: appletInfoData._id,
			year: cYear,
			month: cMonth,
			day: cDay,
			type: shareType,
			count: 1
		})
	} else {
		await db.collection('wmzmall-share-logs').doc(shareLogs._id).update({
			count: dbCmd.inc(1)
		})
		if (shareLogs.count >= shareCount) {
			return result_error('每天限制分享' + shareCount + '次,如需发送给多人,请从好友转发..')
		}
	}

	let wechat_id = appletInfoData.wechat.gh_id
	let alipay_id = appletInfoData.alipay.app_id
	let share_title = "开通会员"
	let share_summary = ''
	let share_path = '/sub-user/pages/member/member-not'
	let share_image = 'https://mp-d84e928e-2b27-4fa9-b240-63574c62e78a.cdn.bspapp.com/QQ截图20240408210058.png'
	let share_web = "https://mall.web.wmeizi.com/skipapplet.html?appId=" + alipay_id
	let memberParameter = await db.collection('wmzmall-member-parameter').where({
		shop_id: appletInfoData._id
	}).get()
	memberParameter = memberParameter.data[0]
	if (!memberParameter) {
		memberParameter = {
			app_id: appletInfoData._id,
			shop_id: appletInfoData._id
		}
		await db.collection('wmzmall-member-parameter').add(memberParameter)
		/* return result_error('未配置会员') */
	}
	if (memberParameter.share_title) {
		share_title = memberParameter.share_title
	}
	if (memberParameter.share_image) {
		share_image = memberParameter.share_image
	}
	if (memberParameter.share_summary) {
		share_summary = memberParameter.share_summary
	}
	return result_success({
		share_title: share_title,
		share_image: share_image,
		share_summary: share_summary,
		_id: memberParameter._id,
		wechat_id: wechat_id,
		share_path: share_path,
		share_web: share_web,
		WXSceneSession: true,
		WXSceneTimeline: false,
	})
}